package com.northwest_man.refactor_tool;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CompareKey {
    private static final String oldDir = Constants.PAT_PROJECT_DIR + "/app/src/main/res";
    private static final String newDir = Constants.HOME_DIR + "/Desktop/AndroidStringFiles";
    // private static final String[] dirs = new String[]{"values", "values-de", "values-el", "values-es", "values-fr",
    //         "values-it", "values-nl", "values-pl", "values-pt", "values-ru", "values-sv", "values-tr"};
            private static final String[] dirs = new String[]{"values", "values-de", };
    private static final Map<String, String> map = new HashMap<>();

    public static void main() throws IOException {
        for (final String dir : dirs) {
            map.put(oldDir + "/" + dir + "/strings.xml", newDir + "/" + dir + "/strings.xml");
        }
        for (final Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(String.format(
                    "------------------------------------%30s------------------------------------",
                    entry.getKey().substring(oldDir.length())));

            final String newFileContent = FileUtils.readFileToString(new File(entry.getValue()),
                    Charset.defaultCharset());
            final String oldFileContent = FileUtils.readFileToString(new File(entry.getKey()),
                    Charset.defaultCharset());

            // <string\s+name\s*=\s*"([^"]+?)"\s*>[\s\S]+?</\s*string\s*>
            final Pattern pattern = Pattern
                    .compile("<string\\s+name\\s*=\\s*\"([^\"]+?)\"\\s*>([\\s\\S]+?)</\\s*string\\s*>");

            final Matcher matcher = pattern.matcher(newFileContent);

            final List<String> newKeyList = new ArrayList<>();
            while (matcher.find()) {
                newKeyList.add(matcher.group(1));
            }
            final Matcher oldMatcher = pattern.matcher(oldFileContent);
            final List<String> oldKeyList = new ArrayList<>();
            final Map<String, String> oldMap = new HashMap<>();
            while (oldMatcher.find()) {
                oldKeyList.add(oldMatcher.group(1));
                oldMap.put(oldMatcher.group(1), oldMatcher.group(2));
            }

            for (final String newKey : newKeyList) {
                boolean found = false;
                for (final String oldKey : oldKeyList) {
                    if (newKey.equals(oldKey)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // System.out.println("新增的 key: " + newKey + "\n");
                }
            }
            final StringBuffer stringBuffer = new StringBuffer();
            for (final String oldKey : oldKeyList) {
                boolean found = false;
                for (final String newKey : newKeyList) {
                    if (newKey.equals(oldKey)) {
                        found = true;
                        break;
                    }

                }
                if (!found) {
                    stringBuffer.append(String.format("<string name=\"%s\">%s</string>", oldKey, oldMap.get(oldKey)));
                }
            }
            if (!stringBuffer.toString().isEmpty()) {
                System.out.println("有缺失字段" + stringBuffer);
                final int endIndex = newFileContent.indexOf("</resources>");
                final String preFileContent = newFileContent.substring(0, endIndex);
                final String sufFileContent = newFileContent.substring(endIndex);

                FileUtils.writeStringToFile(new File(entry.getValue()),
                        preFileContent + stringBuffer.toString() + sufFileContent);
            } else {
                System.out.println("无缺失字段");
            }
        }

    }

}
